package com.bidevalution.controller.api;

import com.bidevalution.business.enums.VoteAlgorithmEnum;
import com.bidevalution.business.enums.VoteTypeEnum;
import com.bidevalution.business.service.BeExpertVoteService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.dao.entity.BeExpertVoteDetailExt;
import com.bidevalution.dao.entity.BeExpertVoteExt;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.support.context.Constants;
import com.qtp.common.constants.SystemConstants;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import com.qtp.core.redis.RedisUtil;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.OConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专家表决控制层 Created by 查慧英 on 2020-04-07.
 */
@Log4j2
@RestController
@RequestMapping("/beExpertVote")
@Api(tags = "专家表决")
public class BeExpertVoteController extends BaseController {
    private static final String USER_GUID = "userGuid";
    private static final String SECTION_ID = "sectionId";
    private static final String TITLE = "title";
    private static final String CONTENT = "content";
    private static final String VOTE_OPTIONS = "voteOptions";
    private static final String VOTE_JSON= "voteJson";
    private static final String SECTION_STEP_ID = "sectionStepId";
    private static final String VOTE_STATUS = "voteStatus";
    private static final String LOGIN_NAME = "loginName";
    @Autowired(required = false)
    BeSectionExpertInfoService beSectionExpertInfoService;
    @Autowired(required = false)
    BeExpertVoteService beExpertVoteService;
    @PostMapping("/callForVote")
    @ApiOperation(value = "专家表决", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = SECTION_ID, value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = TITLE, value = "标题", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = CONTENT, value = "评审内容", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = VOTE_OPTIONS, value = "表决内容", required = true)})
    public Response callForVote(HttpServletRequest request, @RequestParam(SECTION_ID) String sectionId, @RequestParam(TITLE) String title,
                                @RequestParam(CONTENT) String content, @RequestParam(VOTE_OPTIONS) String voteOptions) {
        BeExpertVoteExt expertVote = new BeExpertVoteExt();
        String userGuid = request.getHeader(USER_GUID);
        try {
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                List<BeExpertVoteExt> expertVoteList = new ArrayList<>();
                expertVote.setSectionId(sectionId);
                expertVote.setTitle(title);
                expertVote.setContent(content);
                expertVote.setVoteAlgorithm(VoteAlgorithmEnum.ShaoShuFuCongDuoShu.getCode());
                expertVote.setVoteType(VoteTypeEnum.Manual.getCode());
                expertVote.setVoteOptions(voteOptions);
                expertVote.setOriginator(tsUser.getLoginName());
                List<BeSectionExpertInfo> expertInfoList = beSectionExpertInfoService.getListBySectionId(sectionId, "0");
                List<BeExpertVoteDetailExt> voteDetailList = new ArrayList<>();
                BeExpertVoteDetailExt voteDetail;
                for (BeSectionExpertInfo expertInfo : expertInfoList) {
                    voteDetail = new BeExpertVoteDetailExt();
                    voteDetail.setExpertId(expertInfo.getSeqId());
                    voteDetailList.add(voteDetail);
                }
                expertVote.setVoteDetailList(voteDetailList);
                expertVoteList.add(expertVote);
                return beExpertVoteService.addForVote(expertVoteList);
            } else {
                return Response.fail("获取专家账号登录信息失败！");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("发起表决失败！");
        }
    }

    @PostMapping("/submitVote")
    @ApiOperation(value = "专家表决", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = VOTE_JSON, value = "表决详情json数据", required = true)})
    public Response submitVote(HttpServletRequest request,@RequestParam(VOTE_JSON) String voteJson) {
        try {
            String userGuid = request.getHeader(USER_GUID);
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                return beExpertVoteService.submitVote(voteJson, tsUser.getLoginName());
            } else {
                return Response.fail("获取专家账号登录信息失败！");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("提交表决失败！");
        }
    }
    /**
     * 获取表决列表信息
     *  @param sectionId 标段Id
     */
    @ApiOperation(value = "获取表决列表信息", notes = "获取表决列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/getBeingVoteList")
    public Response<List<Map>> getBeingVoteList(HttpServletRequest request, @RequestParam(SECTION_ID) String sectionId) {
        Map<String, Object> params = new HashMap<>(4);
        String userGuid = request.getHeader(USER_GUID);
        TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
        try {
            if (tsUser != null) {
                params.put(SECTION_ID, sectionId);
                params.put(VOTE_STATUS, 10);
                params.put(LOGIN_NAME, tsUser.getLoginName());
                return beExpertVoteService.getBeingVoteList(params);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
    /**
     * 获取表决记录列表信息
     * @param sectionId 标段Id
     */
    @ApiOperation(value = "获取表决记录列表信息", notes = "获取表决记录列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = SECTION_STEP_ID, value = "标段环节Id", paramType = "query", dataType = "String")
    })
    @PostMapping("/getVotingRecordList")
    public Response<List<Map>> getVotingRecordList(HttpServletRequest request,@RequestParam(SECTION_ID) String sectionId) {
        Map<String, Object> params = new HashMap<>(4);
        String sectionStepId = request.getParameter(SECTION_STEP_ID);
        String userGuid = request.getHeader(USER_GUID);
        TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
        try {
            if (tsUser != null) {
                params.put(SECTION_ID, sectionId);
                params.put(VOTE_STATUS, 20);
                if (OConvertUtils.isNotEmpty(sectionStepId)) {
                    params.put(SECTION_STEP_ID, sectionStepId);
                }
                params.put(LOGIN_NAME, tsUser.getLoginName());
                return beExpertVoteService.getVotingRecordList(params);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
    @PostMapping("/testVote")
    @ApiOperation(value = "专家表决", response = String.class)
    public Response<List<Map>> testVote(HttpServletRequest request) {
        try {

            return beExpertVoteService.toUpdateTheVote(null);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("提交表决失败！");
        }
    }

    /**
     * 获取表决提示信息
     * @param sectionId 标段Id
     */
    @ApiOperation(value = "获取表决提示信息", notes = "获取表决提示信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/getVoteMsg")
    public Response getVoteMsg(HttpServletRequest request, @RequestParam(SECTION_ID) String sectionId) {
        String userGuid = request.getHeader(USER_GUID);
        TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
        if(tsUser != null){
            String loginName = tsUser.getLoginName();
            String key = String.format(Constants.VOTE_MSG_TEMPLATE, sectionId, loginName);
            String val = RedisUtil.get(key);
            if (OConvertUtils.isNotEmpty(val)){
                return Response.ok(GsonUtil.jsonToMapWithGson(val));
            }
            return Response.ok();
        }else {
            return Response.fail("未登录！");
        }
    }
    /**
     * 确认表决提示信息
     * @param sectionId 标段Id
     */
    @ApiOperation(value = "确认表决提示信息", notes = "确认表决提示信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/confirmVoteMsg")
    public Response confirmVoteMsg(HttpServletRequest request, @RequestParam(SECTION_ID) String sectionId) {
        String userGuid = request.getHeader(USER_GUID);
        TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
        if(tsUser != null){
            String loginName = tsUser.getLoginName();
            String key = String.format(Constants.VOTE_MSG_TEMPLATE, sectionId, loginName);
            return Response.ok(RedisUtil.delete(key));
        }else {
            return Response.fail("未登录！");
        }
    }

}
